//
//  ReadWindow.m
//  ePub
//
//  Created by linziyuan on 17/3/13.
//  Copyright © 2017年 creative. All rights reserved.
//

#import "ReadWindow.h"


@implementation ReadWindow

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+ (instancetype)showReadWindowWithframe:(CGRect)frame rootVC:(UIViewController *)rootVC{
    if (!rootVC) {
        return nil;
    }
    ReadWindow *readWindow = [[ReadWindow alloc] initWithFrame:frame];
    readWindow.windowLevel = UIWindowLevelNormal;
    readWindow.rootViewController = rootVC;
    readWindow.tag = ReadWindowTag;
    [readWindow makeKeyAndVisible];
    return readWindow;
}

- (void)dissMissWindowWithOrientation:(UIInterfaceOrientation)orientation {
    UIInterfaceOrientation backOrientaion = UIInterfaceOrientationPortrait;
    if (orientation) {
        backOrientaion = orientation;
    }
    [self toRotation:@(orientation)];
    dispatch_time_t time=dispatch_time(DISPATCH_TIME_NOW, 0.3*NSEC_PER_SEC);
    dispatch_after(time, dispatch_get_main_queue(), ^{
        [self resignKeyWindow];
        [self removeFromSuperview];
    });
}

- (void)toRotation:(NSNumber*)rtn
{
    if (![NSThread isMainThread]) {
        [self performSelectorOnMainThread:@selector(toRotation:) withObject:rtn waitUntilDone:NO];
        return;
    }
    UIInterfaceOrientation rotation = [rtn integerValue];
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = rotation;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];//perform 的传参表达方式
    }
}

@end
